home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: help:what is wrong with this code?
- Date: 14 Apr 1996 18:56:23 -0700
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4ksac7INNk1g@keats.ugrad.cs.ubc.ca>
- References: <4krmdu$t3h@brahms.udel.edu>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4krmdu$t3h@brahms.udel.edu>,
- Yue-hong Zheng <yuehong@brahms.udel.edu> wrote:
-
- Sorry, I don't feel like helping you, but I will at least format the code
- for you so it has proper indentation and token spacing for clarity. It's the
- least I can do for a fellow programmer. I will do this by piping through the
- GNU indent program with the -kr option (guess what style _that_ is).
-
- #include <stdio.h>
-
- void quicksort(int[], int, int);
-
- void swap(int *, int *);
-
- main()
- {
- int k;
- int a[] =
- {1, 3, 4, 2, 43, 23, 5, 6, 87, 92, 21};
- quicksort(a, 0, 10);
- for (k = 0; k <= 10; k++) {
- printf("%d\n", a[k]);
- }
- return 0;
- }
-
- void swap(int *s, int *t)
- {
- int tmp;
- tmp = *s;
- *s = *t;
- *t = tmp;
- }
-
- void quicksort(int array[], int left, int right)
- {
- int i, j, median;
- if (right <= left)
- return;
- median = (right + left) / 2;
- swap(&array[median], &array[right]);
- i = left;
- j = right - 1;
- while ((j - i) >= -1) {
- while ((array[i] < array[right]) && (i <= (right - 1)))
- i++;
- while ((array[j] > array[right]) && (j >= 0))
- j--;
- swap(&array[i], &array[j]);
- }
- swap(&array[i], &array[right]);
- quicksort(array, left, (i - 1));
- quicksort(array, (i + 1), right);
- }
-
-
- Never post unindented code again unless it's unintended. Or vice versa.
- --
- I'm not really a jerk, but I play one on Usenet.
-